package com.yunqing.restkeeper.store.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName MybatisPlusTenantConfig
 * @Description TODO
 * @author: puyunqing
 * @Date: 2025/7/21
 */
@Configuration
@Slf4j
public class MybatisPlusTenantConfig {

    // 设置多租户字段
    private static final String SYSTEM_TENANT_SHOPID = "shop_id";
    private static final String SYSTEM_TENANT_STOREID = "store_id";

    // 有哪些表会忽略多租户操作
    private static final List<String> IGNORE_TENANT_TABLES = List.of("t_staff");

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){

        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        // 为 shop_id 添加租户拦截器
        TenantLineInnerInterceptor shopInterceptor = new TenantLineInnerInterceptor();

        shopInterceptor.setTenantLineHandler(new TenantLineHandler() {
            @Override
            public Expression getTenantId() {
                log.info("MybatisPlusTenantConfig-----------shopId-----------" + RpcContext.getServerAttachment().getAttachment("shopId"));
                String shopId = RpcContext.getServerAttachment().getAttachment("shopId");
                if (shopId == null) {
                    throw new RuntimeException("get shopId error");
                }
                return new StringValue(shopId);
            }

            @Override
            public String getTenantIdColumn() {
                return SYSTEM_TENANT_SHOPID;
            }

            @Override
            public boolean ignoreTable(String tableName) {
                return IGNORE_TENANT_TABLES.stream()
                        .anyMatch(e -> e.equalsIgnoreCase(tableName));
            }

        });

        // 为 store_id 添加租户拦截器
        TenantLineInnerInterceptor storeInterceptor = new TenantLineInnerInterceptor();

        storeInterceptor.setTenantLineHandler(new TenantLineHandler() {

            @Override
            public Expression getTenantId() {
                String storeId = RpcContext.getServerAttachment().getAttachment("storeId");
                if (storeId == null) {
                    throw new RuntimeException("get storeId error");
                }
                return new StringValue(storeId);
            }

            @Override
            public String getTenantIdColumn() {
                return SYSTEM_TENANT_STOREID;
            }

            @Override
            public boolean ignoreTable(String tableName) {
                return IGNORE_TENANT_TABLES.stream()
                        .anyMatch(e -> e.equalsIgnoreCase(tableName));
            }

        });

        // 将两个租户拦截器添加到 MybatisPlusInterceptor 中
        interceptor.addInnerInterceptor(shopInterceptor);
        interceptor.addInnerInterceptor(storeInterceptor);

        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加

        return interceptor;
    }
}
